﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView.EmpBasicManager
{
    public partial class EmpEducationWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private EmpbasicService.Emp_EducationModel model = new EmpbasicService.Emp_EducationModel();
        private EmpbasicService.Emp_EducationModel oldmodel = new EmpbasicService.Emp_EducationModel();
        private InsertSystemRemark insertsys = new InsertSystemRemark();
        public int ID;
        public int EmpID;
        public bool IsNew = false;
        public EmpEducationWindow(int empID, int iD, bool isnew)
        {
            InitializeComponent();
            ID = iD;
            EmpID = empID;
            IsNew = isnew;
            if (IsNew)
            {
                this.Title = "新增学历信息";
                model = new EmpbasicService.Emp_EducationModel();
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑学历信息";
                Client.GetEmp_EducationModelCompleted += new EventHandler<EmpbasicService.GetEmp_EducationModelCompletedEventArgs>(Client_GetEmp_EducationModelCompleted);
                Client.GetEmp_EducationModelAsync("(" + EmpID + ")", ID);
            }
        }

        void Client_GetEmp_EducationModelCompleted(object sender, EmpbasicService.GetEmp_EducationModelCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                model = e.Result[0];
                oldmodel = new EmpbasicService.Emp_EducationModel
                {
                    EducationName = model.EducationName,
                    EducationType = model.EducationType,
                    EducationaSystem = model.EducationaSystem,
                    GraduationDate = model.GraduationDate,
                    School = model.School,
                    Professional = model.Professional,
                    IsTopEducation = model.IsTopEducation
                };
            }
            this.DataContext = model;
            if (model.IsTopEducation == true)
            {
                check.IsChecked = true;
            }
            else
            {
                check.IsChecked = false;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (check.IsChecked == true)
            {
                model.IsTopEducation = true;
            }
            else
            {
                model.IsTopEducation = false;
            }
            if (IsNew == true)
            {
                model.EmpBasicID = EmpID;
                
                Client.AddEmp_EducationModelCompleted += new EventHandler<EmpbasicService.AddEmp_EducationModelCompletedEventArgs>(Client_AddEmp_EducationModelCompleted);
                Client.AddEmp_EducationModelAsync(model);
            }
            else
            {
                if (!string.IsNullOrEmpty(begin.Text)) model.GraduationDate = Convert.ToDateTime(begin.Text);
                else
                    model.GraduationDate = null;
                Client.UpdateEmp_EducationModelCompleted += new EventHandler<EmpbasicService.UpdateEmp_EducationModelCompletedEventArgs>(Client_UpdateEmp_EducationModelCompleted);
                Client.UpdateEmp_EducationModelAsync(model);
            }
        }

        void Client_UpdateEmp_EducationModelCompleted(object sender, EmpbasicService.UpdateEmp_EducationModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                InsertSystem("编辑");
                this.DialogResult = true;
            }
        }

        void Client_AddEmp_EducationModelCompleted(object sender, EmpbasicService.AddEmp_EducationModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                InsertSystem("新增");
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        void InsertSystem(string FangShi)
        {
            if (FangShi == "新增")
            {
                insertsys.Insert("学历子集", "", "", model.EmpBasicID, FangShi, "");
            }
            else
            {
                if (oldmodel.GraduationDate != model.GraduationDate)
                    insertsys.Insert("学历子集", oldmodel.GraduationDate == null ? null : Convert.ToDateTime(oldmodel.GraduationDate).ToShortDateString(),
                        model.GraduationDate == null ? null : Convert.ToDateTime(model.GraduationDate).ToShortDateString(),
                        model.EmpBasicID, FangShi, "毕（肄）业日期");
                if (oldmodel.EducationName != model.EducationName)
                    insertsys.Insert("学历子集", oldmodel.EducationName, model.EducationName,
                        model.EmpBasicID, FangShi, "学历名称");
                if (oldmodel.EducationType != model.EducationType)
                    insertsys.Insert("学历子集", oldmodel.EducationType, model.EducationType,
                        model.EmpBasicID, FangShi, "学习形式");

                if (oldmodel.EducationaSystem != model.EducationaSystem)
                    insertsys.Insert("学历子集", oldmodel.EducationaSystem, model.EducationaSystem,
                        model.EmpBasicID, FangShi, "学制");
                if (oldmodel.School != model.School)
                    insertsys.Insert("学历子集", oldmodel.School, model.School,
                        model.EmpBasicID, FangShi, "学校");
                if (oldmodel.Professional != model.Professional)
                    insertsys.Insert("学历子集", oldmodel.Professional, model.Professional,
                        model.EmpBasicID, FangShi, "所学专业");
                if (oldmodel.IsTopEducation != model.IsTopEducation)
                    insertsys.Insert("学历子集", oldmodel.IsTopEducation == false ? "否" : "是", model.IsTopEducation == false ? "否" : "是",
                        model.EmpBasicID, FangShi, "是否最高学历");
            }

        }
    }
}
